package com.lyn.pluginhost.service.binder

import android.app.Application
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.lyn.pluginapi.entity.PluginData
import com.lyn.pluginapi.entity.PluginInfo
import com.lyn.pluginapi.gson.MY_GSON
import com.lyn.pluginhost.GetLogoResultCallbackAidlInterface
import com.lyn.pluginhost.PluginAidlInterface
import com.lyn.pluginhost.plugin.PluginManager
import com.lyn.pluginhost.ResultCallbackAidlInterface
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 *
 * @author longyn 2025/07/10
 * @version 1.0.0
 */
class PluginHostBinder(private val application: Application) : PluginAidlInterface.Stub() {

    companion object{
        const val TAG  = "PluginHostBinder"
    }

    val coroutineScope:CoroutineScope = CoroutineScope(Dispatchers.IO)
    val pluginLiveData:MutableLiveData<MutableList<PluginInfo>> = MutableLiveData()

    override fun initialize(resultCallback: ResultCallbackAidlInterface?) {
        Log.d(TAG,"initialize")
        if (PluginManager.instance.initialize(application)) {
            resultCallback?.onSuccess("success")
        } else {
            resultCallback?.onFail(-1, "fail")
        }
    }

    override fun loadPlugin(path: String?, resultCallback: ResultCallbackAidlInterface?) {
        val pluginPath = path ?: ""
        coroutineScope.launch(Dispatchers.IO){
            val pluginInfo = PluginManager.instance.loadPlugin(pluginPath)
            if (pluginInfo == null){
                resultCallback?.onFail(-1,"插件加载失败")
            }else{
                resultCallback?.onSuccess(pluginInfo.toString())
            }
        }

    }

    override fun loadAllPlugin(resultCallback: ResultCallbackAidlInterface?) {
        coroutineScope.launch(Dispatchers.IO){
            val pluginInfos = PluginManager.instance.loadAllPlugin()
            pluginLiveData.postValue(pluginInfos)
            val pluginData = PluginData(pluginInfos)
            val json = MY_GSON.toJson(pluginData)
            resultCallback?.onSuccess(json)
        }
    }

    override fun getAllPlugin(resultCallback: ResultCallbackAidlInterface?) {
        resultCallback?.let {
            pluginLiveData.value?.let { pluginInfos->
                val pluginData = PluginData(pluginInfos)
                val json = MY_GSON.toJson(pluginData)
                it.onSuccess(json)
                return
            }
            it.onFail(-1,"没有获取到插件")
        }
    }

    override fun getPluginLogo(
        id: String?,
        path: String?,
        getLogoResultCallback: GetLogoResultCallbackAidlInterface?
    ) {
        coroutineScope.launch {
            val bytes = PluginManager.instance.getPluginLogo(id ?: "", path ?: "")
            getLogoResultCallback?.let { it.onSuccess(bytes)
                return@launch
            }
        }
    }


    override fun removePlugin(id: String?, resultCallback: ResultCallbackAidlInterface?) {
        coroutineScope.launch {
            pluginLiveData.value?.let { pluginInfos->
                pluginInfos.forEach { pluginInfo->
                    if (pluginInfo.id == id){
                        PluginManager.instance.removePlugin(pluginInfo)
                        resultCallback?.onSuccess("success")
                        return@launch
                    }
                }
                resultCallback?.onFail(-1,"没有找到可删除的插件")
                return@launch
            }
            resultCallback?.onFail(-1,"没有找到可删除的插件")
        }
    }

    override fun openPlugin(id: String?, resultCallback: ResultCallbackAidlInterface?) {
        pluginLiveData.value?.let { pluginInfos->
            pluginInfos.forEach { pluginInfo ->
                if ((id ?: "") == pluginInfo.id){
                    if (PluginManager.instance.openPlugin(pluginInfo.id,pluginInfo.launcherClassName)){
                        resultCallback?.onSuccess("success")
                    }else{
                        resultCallback?.onFail(-1,"fail")
                    }
                    return
                }
            }
        }
        resultCallback?.onFail(-1,"fail")
    }

    override fun closePlugin(id: String?, resultCallback: ResultCallbackAidlInterface?) {
        PluginManager.instance.closePlugin(id?:"")
        resultCallback?.onSuccess("success")
    }
}